<?php

namespace app\api\model;

use app\admin\model\Shop;
use app\common\model\Config;
use think\Db;
use think\Model;

class ShopSettlement extends Model
{
    // 表名
    protected $name = 'shop_settlement';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;


    public function settlement($end_time)
    {
        //获取最近的店铺结算时间
        $start_time = \app\common\model\Config::get(['name'=>'shop_settlement_time'])->value;
        if ($end_time - $start_time < 3600 * 20) {
            return true;
        }
        $settlement_model = new ShopSettlement();
        //店铺列表
        $shop_list = Shop::field('id,shop_name')->select();

        Db::startTrans();
        try {
            //循环各个店铺数据
            foreach ($shop_list as $k => $store) {
                $settlement_where = [
                    'order_status'=>1,
                    'is_settlement'=>0,
                    'shop_id'=>$store['id']
                ];
                $order_money = Order::where($settlement_where)
                    ->where('complete_time','<=',$end_time)
                    ->sum('order_money');
                $shop_money = Order::where($settlement_where)
                    ->where('complete_time','<=',$end_time)
                    ->sum('pay_money');
                $refund_shop_money = Order::where($settlement_where)
                    ->where('complete_time','<=',$end_time)
                    ->sum('refund_money');
                $cashback_money = Order::where($settlement_where)
                    ->where('complete_time','<=',$end_time)
                    ->sum('cashback_money');
                $settlement          = [
                    'settlement_no'        => date('YmdHi') . $store['id'] . rand(1111, 9999),
                    'shop_id'              => $store['id'],
                    'shop_name'            => $store['shop_name'],
                    'order_money'          => $order_money,
                    'shop_money'         => $shop_money,
                    'refund_shop_money'       => $refund_shop_money,
                    'cashback_money'         => $cashback_money,
                    'start_time'           => $start_time,
                    'end_time'             => $end_time
                ];
                $settlement_model->save($settlement);
                $shop_settlement_id = $settlement_model->id;
                Order::where(['order_status'=>1, 'is_settlement'=>0, 'shop_id'=>$store['id']])
                    ->where('complete_time','<=',$end_time)
                    ->update(['shop_settlement_id' => $shop_settlement_id, 'is_settlement' => 1,'settlement_time'=>time()]);

            }
            $config_model = new Config();
            $data = [
                //'name'=>'yly',
                'group'=>'settlement',
                'title'=>'结算时间',
                'type'=>'number',
                'value'=>$end_time
            ];

            $res = $config_model->save($data,['name'=>'shop_settlement_time']);
            //model('shop')->update(['store_settlement_time' => $end_time], [['site_id', '=', $site_id]]);

            Db::commit();
            return true;
        }catch (\Exception $e) {
            Db::rollback();
            throw new \Exception($e->getMessage());
        }

    }
}